// 导入常量，用于判断执行方法
import { 
  ADD_NUMBER, 
  SUB_NUMBER,
  INCREMENT,
  DECREMENT
} from './constants.js'

// 定义基础状态
const initState = {
  counter: 0
}

// 定义reducer，执行改变state
function reducer(state = initState, action) {
  switch(action.type) {
    case ADD_NUMBER:
      return {...state, counter: state.counter + action.num}
    case SUB_NUMBER:
      return {...state, counter: state.counter - action.num}
    case INCREMENT:
      return {...state, counter: state.counter + 1}
    case DECREMENT:
      return {...state, counter: state.counter - 1}
    default:
      return state
  }
}

export default reducer;
